'use strict';
const Controller = require('egg').Controller;
//环境默认参数
class ConfigController extends Controller {
   async add(){
        const {ctx,app,service} = this;
        var name = ctx.request.body.name || "";
        if(!name){
            ctx.error(app.resCode.params);
        }else{
            try{
                let res = await service.config.add(name);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
   }
   async remove(){
       const {ctx,app,service} = this;
       let id = ctx.request.body.id || "";
       if(!id){
        ctx.error(app.resCode.params);
       }else{
            try{
                let res = await service.config.remove(id);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
       }
   }
   async update(){
        const {ctx,app,service} = this;
        let param = ctx.request.body;
        let id = param.id;
        let data = param.data;
        if(!id){
            ctx.error(app.resCode.params);
        }else{
            try{
                let res = await service.config.update(id,data);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
   }
   async list(){
    const {ctx,app,service} = this;
    try{
        let res = await service.config.list();
        ctx.success({list:res,currEnv:process.env.NODE_ENV});
    }catch(e){
        ctx.error(app.resCode.params,e.message || e);
    }
   }
   async info(){
    const {ctx,app,service} = this;
    let id = ctx.query.id;
    if(!id){
        ctx.error(app.resCode.params);
    }else{
        try{
            let res = await service.config.info(id);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        } 
    }
   }
}

module.exports = ConfigController;